<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/12/31
 * Time: 14:57
 */

namespace Xbd\Controller;


use Admin\Api\ConfigApi;
use Admin\Api\PostApi;
use Shop\Api\BannersApi;
use Xbd\Api\PostInfoApi;

class IndexController extends XbdController {

    protected function _initialize(){
        parent::_initialize();

        $this->getNav();
        $this->getNotice();
        $this->getRecommend();
        $this->getRightList();
        $this->getAd();
    }

    /**
     * 首页
     */
    public function index(){

        $page = array('curpage'=>I('get.p',0),'size'=>10);

        $map['post_position'] = array('NEQ',2);

        $order = "post_date desc";

        $result = apiCall(PostInfoApi::QUERY,array($map,$page,$order));

        if($result['status']){

            $this->assign("show",$result['info']['show']);
            $this->assign("list",$result['info']['list']);
            $this->display();
        }else{
            $this->error($result['info']);
        }

    }

    /**
     * 分类页，
     */
    public function cate(){

        $cate_id = I('get.id','');

        $page = array('curpage'=>I('get.p',0),'size'=>10);

        $map = array(
            'post_category'=>$cate_id,
        );

        $order = "post_date desc";

        $result = apiCall(PostInfoApi::QUERY,array($map,$page,$order));

        if($result['status']){

            $this->assign("show",$result['info']['show']);
            $this->assign("list",$result['info']['list']);
            $this->assign('category',$result['info']['list'][0]['post_cate_name']);
            $this->display();
        }else{
            $this->error($result['info']);
        }

    }

    /**
     * 详情页
     */
    public function view(){

        if (IS_POST){
            $this->success('信息提交成功!');
        }

        $id = I('id',-1,'intval');

        $map['id']=$id;

        //增加浏览量
        $field = "post_view";

        $res =  apiCall(PostApi::UPDATE_VIEW,array($map,$field));
        if($res['status']===false){
            $this->error($res['info']);
        }

        $result = apiCall(PostInfoApi::GET_INFO,array($map));


        if($result['status']){

            //$this->assign('content',$result['info']['post_content']);
            $this->assign('content',htmlspecialchars_decode($result['info']['post_content']));
            $this->assign('title',$result['info']['post_title']);
            $this->assign('date',$result['info']['post_date']);
            $this->assign('view',$result['info']['post_view']);
            $this->assign("show",$result['info']['show']);
            $this->assign("list",$result['info']['list']);
        }else{
            $this->error($result['info']);
        }

        $this->display();
    }


    public function find_p(){
        $map =  array(
            'post_category'=>'6012',
        );

        $result = apiCall(PostInfoApi::QUERY,array($map));

        if($result['status']){
            $this->assign('cases',$result['info']['list']);
            $this->assign('show',$result['info']['show']);
        }
        $this->display();
    }

    public function about(){
        $map =  array(
            'post_category'=>'6017',
        );

        $result = apiCall(PostInfoApi::GET_INFO,array($map));
        if($result['status']){
            $this->assign('about',$result['info']);
        }
        $this->display();
    }

    public function zc(){
        $this->display();
    }


    //***私有不公开


    /**
     * 获取导航信息
     * @author 老胖子-何必都 <hebiduhebi@126.com>
     */
    private function getNav(){

        $nav = array(
            array(
                'id'=>'1',
                'name'=>'政策资讯',
                'href'=>U('Xbd/Index/find_p'),
                '_child'=>array()),
            array('name'=>'返乡好项目',
                'id'=>'11',
                '_child'=>array(
                    array('id'=>'12','name'=>'寻找好项目','href'=>U('Xbd/Index/find_p')),
                    array('id'=>'12','name'=>'我有好项目','href'=>U('Xbd/Index/submit_p'))
                )
            ),
            array('name'=>'项目众创',
                'id'=>'21',
                '_child'=>array(
                    array('id'=>'22','name'=>'项目众筹',),
                    array('id'=>'23','name'=>'项目策划',),
                    array('id'=>'24','name'=>'技术开发',),
                    array('id'=>'25','name'=>'运营推广',),
                    array('id'=>'26','name'=>'人才培训',),
                    array('id'=>'27','name'=>'众创社区',)
                )
            ),
            array('name'=>'产品众筹',
                'id'=>'31',
                'href'=>U('Xbd/Index/zc'),
                '_child'=>array(
                    array('id'=>'32','name'=>'轻 众 筹','href'=>U('Xbd/Index/zc'),),
                    array('id'=>'33','name'=>'众筹社区','href'=>U('Xbd/Index/zc'),),
                )
            ),
            array('name'=>'投融资',
                'id'=>'41',
                'href'=>U('Xbd/Index/dk'),
                '_child'=>array(
                    array('id'=>'42','name'=>'我要贷款','href'=>U('Xbd/Index/dk'),),
                    array('id'=>'43','name'=>'我要投资','href'=>U('Xbd/Index/dk'),),
                )
            ),
            array('name'=>'创富学堂',
                'id'=>'51',
                '_child'=>array(
                    array('id'=>'52','name'=>'创富公开课',),
                    array('id'=>'53','name'=>'创富沙龙',),
                    array('id'=>'54','name'=>'创富集中营',),
                    array('id'=>'55','name'=>'小不懂空间',),
                )
            ),
            array('name'=>'联系我们',
                'id'=>'61',
                'href'=>U('Xbd/Index/about'),
                '_child'=>array(
                )
            ),

        );

        $this->assign('nav_list',$nav);

    }


    private function getNotice(){
        $map = array(
            'post_category'=>6017,
        );
        $order['post_modified'] = "desc";
        $limit = 10;
        $result = apiCall(PostInfoApi::QUERY_LIMIT,array($map,$order,$limit));
        if($result['status'] && is_array($result['info'])){
            $this->assign('notice',$result['info']);
            //dump($result);
        }else{
            $this->assign('notice','欢迎光临!');
        }
    }

    /**
     * 获取推荐文章列表
     */
    private function getRecommend(){

        $map['post_position'] = "2";

        $order['post_modified'] = "desc";

        $limit = 6;

        $result = apiCall(PostInfoApi::QUERY_LIMIT,array($map,$order,$limit));

        if($result['status']){
            $this->assign('recommend',$result['info']);
        }else{
            $this->error($result['info']);
        }


        /*echo "<pre>";
        var_dump($result);
        echo "</pre>";*/
    }

    /**
     * 获得右边文章列表
     */
    private function getRightList(){

        $map=array();

        $order['post_date'] = 'desc';

        $limit = 10;

        $res = apiCall(PostInfoApi::QUERY_LIMIT,array($map,$order,$limit));
        //dump($res['info']);
        if($res['status']){
            $this->assign("right_list",$res['info']);
        }else{
            $this->error($res['info']);
        }
    }

    /**
     * 获得广告相关信息
     */
    private function getAd(){
        //*****首页顶部广告和导航栏下广告*****
        $m1=array('position'=>getDatatree('TOP_AD_POSITION'));
        $order1="createtime desc";

        $res1=apiCall(BannersApi::GET_INFO,array($m1,$order1));

        if($res1['status']){
            //dump($res['info']);
            $this->assign('top_ad',$res1['info']);
        }else{
            $this->error($res1['info']);
        }

        $m2=array('position'=>getDatatree('NAV_AD_POSITION'));
        $order2='createtime desc';

        $res2=apiCall(BannersApi::GET_INFO,array($m2,$order2));

        if($res2['status']){
            $this->assign('nav_ad',$res2['info']);
        }else{
            $this->error($res2['info']);
        }

        $m3 = array('position'=>getDatatree('TOP_AD_RIGHT_POSITION'));
        $order3 = "createtime desc";

        $res3 = apiCall(BannersApi::GET_INFO,array($m3,$order3));

        if($res3['status']){
            $this->assign('top_right_ad',$res3['info']);
        }else{
            $this->error($res3['info']);
        }
    }


}

